#/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2022/1/11 9:59
# @Author  : wyy
# @Email   : 1915059545@qq.com
# @File    : page_tjcp.py
# @Software: PyCharm
"""添加产品页面"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
from pages.base_page import BasePage
from time import sleep
class PageTianJiaChanPin(BasePage):
    username_locator = (By.NAME, "name")
    password_locator = (By.NAME, "password")
    submit_locator = (By.NAME, "submit")
    # 产品名称
    cpmc_locator = (By.NAME, "name")
    # 研发时间
    yfsj_locator = (By.NAME, "development_time")
    # 开发团队
    kftd_locator = (By.NAME, "development_team")
    # 建议售价
    jysj_locator = (By.NAME, "suggested_price")
    # 产品类别
    cplb_locator = (By.NAME, "category_id")
    # 详情链接
    xqlj_locator = (By.NAME, "link")
    # 成本价
    cbj_locator = (By.NAME, "cost_price")
    # 备注
    bz_locator = (By.CLASS_NAME, "span6")
    # 保存
    bc_locator = (By.XPATH, '//*[@id="form1"]/table/tfoot/tr/td/input[1]')
    # 选择文件
    xzwj_locator = (By.NAME, "main_pic[]")
    #输入产品名称
    def input_cpmc(self,cpmc):
        self.driver.find_element(*self.cpmc_locator).send_keys(cpmc)
    #编辑
    def input_cpmc1(self,cpmc):
        self.driver.find_element(*self.cpmc_locator).clear()
        self.driver.find_element(*self.cpmc_locator).send_keys(cpmc)
    #输入研发时间,点一下
    def input_yanfatime(self,yftime):
        self.driver.find_element(*self.yfsj_locator).send_keys(yftime)
        action = ActionChains(self.driver)
        action.move_by_offset(60, 90).click().perform()
    #输入开发团队
    def input_kftd(self,kftd):
        self.driver.find_element(*self.kftd_locator).send_keys(kftd)
    #输入建议售价
    def input_jysj(self,jysj):
        self.driver.find_element(*self.jysj_locator).send_keys(jysj)
    #输入详情链接
    def input_xqlj(self,xqlj):
        self.driver.find_element(*self.xqlj_locator).clear()
        self.driver.find_element(*self.xqlj_locator).send_keys(xqlj)
    #输入成本价
    def input_cbj(self,cbj):
        self.driver.find_element(*self.cbj_locator).send_keys(cbj)
    #上传文件
    def input_xzwj(self,wenjian):
        self.driver.find_element(*self.xzwj_locator).send_keys(wenjian)
    #输入备注
    def input_bz(self,bz):
        self.driver.find_element(*self.bz_locator).send_keys(bz)
    #点击保存
    def click_bc(self):
        self.driver.find_element(*self.bc_locator).click()
    #清理数据
    def delete_gouxuan_dange(self,cpmc):
        # 定位产品列表
        cp_table_loccator = (By.XPATH, '//*[@id="form1"]/table/tbody')
        table = self.driver.find_element(*cp_table_loccator)
        trs = table.find_elements(By.TAG_NAME, "tr")
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, "td")
            if tds[2].text == (cpmc):
                tds[0].find_element(By.TAG_NAME, "input").click()
                break
        # 删除
        sc_locator = (By.ID, "delete")
        self.driver.find_element(*sc_locator).click()
        self.driver.switch_to.alert.text
        self.driver.switch_to.alert.accept()
    def cp_duanyan(self):
        dy_locator=(By.XPATH,'//*[@id="form1"]/table/tbody/tr[1]/td[3]/a/span')
        return self.driver.find_element(*dy_locator).text
    #添加产品依次合法添加
    def add_product(self,cpmc,yfsj,kftd,jysj,xqlj,cbj,bz,cpmc1):
        self.input_cpmc(cpmc)
        self.input_yanfatime(yfsj)
        self.input_kftd(kftd)
        self.input_jysj(jysj)
        self.input_xqlj(xqlj)
        self.input_cbj(cbj)
        self.input_bz(bz)
        sleep(2)
        self.click_bc()
        sleep(2)
        self.cp_duanyan()
        self.delete_gouxuan_dange(cpmc1)
        sleep(2)
    # 添加产品必填项合法添加
    def addbtx_product(self,cpmc,kftd,cpmc1):
        self.input_cpmc(cpmc)
        self.input_kftd(kftd)
        sleep(2)
        self.click_bc()
        sleep(2)
        self.cp_duanyan()
        self.delete_gouxuan_dange(cpmc1)
        sleep(2)